package com.example.ruiji.cotroller;

import com.alibaba.excel.EasyExcel;
import com.example.ruiji.pojo.Category;
import com.example.ruiji.service.CategoryService;
import com.example.ruiji.util.CategoryListener;
import com.example.ruiji.util.Code;
import com.example.ruiji.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping(value = "/category", produces = "application/json;charset=UTF-8")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @RequestMapping("/list")
    public Result list() {
        List<Category> all = categoryService.getAll();
        System.out.println(all);
        if (all != null) {
            return new Result(Code.GET_OK, all, "查询成功");
        } else {
            return new Result(Code.GET_ERR, "查询失败");
        }
    }

    @RequestMapping("/insert")
    public Result insert(@RequestBody Category category) {
        int i = categoryService.insertcaipin(category);
        if (i > 0) {
            return new Result(Code.SAVE_OK, "新增成功");
        } else {
            return new Result(Code.SAVE_ERR, "新增失败");
        }
    }

    @RequestMapping("/inserttaocan")
    public Result inserttaocan(@RequestBody Category category) {
        System.out.println(category);
        int i = categoryService.inserttaocan(category);
        if (i > 0) {
            return new Result(Code.SAVE_OK, "新增成功");
        } else {
            return new Result(Code.SAVE_ERR, "新增失败");
        }
    }

    @RequestMapping("/delete/{name}")
    public Result delete(@PathVariable("name") String id) {
        int i = categoryService.delete(id);
        if (i > 0) {
            return new Result(Code.DELETE_OK, "删除成功");
        } else {
            return new Result(Code.DELETE_ERR, "删除失败");
        }
    }

    @RequestMapping("/update")
    public Result update(@RequestBody Category category) {
        long l = category.getId() / 100;
        category.setId(l);
        int i = categoryService.update(category);
        if (i > 0) {
            return new Result(Code.UPDATE_OK, "修改成功");
        } else {
            return new Result(Code.UPDATE_ERR, "修改失败");
        }
    }

    @Autowired
    private CategoryListener categoryListener;

    @PostMapping(value = "/importData")
    public Result importData(MultipartFile file) {
        try {
            //获取文件的输入流
            InputStream inputStream = file.getInputStream();
            List<Category> lst = EasyExcel.read(inputStream) //调用read方法
                    //注册自定义监听器，字段校验可以在监听器内实现
                    .registerReadListener(categoryListener)
                    .head(Category.class) //对应导入的实体类
                    .sheet(0) //导入数据的sheet页编号，0代表第一个sheet页，如果不填，则会导入所有sheet页的数据
                    .headRowNumber(1) //列表头行数，1代表列表头有1行，第二行开始为数据行
                    .doReadSync(); //开始读Excel，返回一个List<T>集合，继续后续入库操作

            //模拟导入数据库操作
            for (Category userDO : lst) {
                System.out.println(userDO.toString());
            }

            return new Result(Code.GET_OK, "导入成功");


        } catch (IOException exception) {
            exception.printStackTrace();
            return new Result(Code.GET_ERR, "导入失败");
        }
    }


    @PostMapping("/daochu")
    public ResponseEntity<byte[]> daochu() {
        List<Category> all = categoryService.getAll();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        EasyExcel.write(outputStream, Category.class).sheet("食品信息表").doWrite(all);
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=food.xlsx");
        return ResponseEntity.ok()
                .headers(headers).contentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
                .body(outputStream.toByteArray());


    }

}
